package com.environment_pro.monitor.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.environment_pro.monitor.controller.BaseController;
import com.environment_pro.monitor.service.TbSystemConfService;
import com.environment_pro.monitor.util.StaticUtil;

public class SecurityInterceptor extends HandlerInterceptorAdapter {
	
	// 继承HandlerInterceptorAdapter类

	private static final Logger logger = Logger.getLogger(SecurityInterceptor.class); 
	
	
	//在业务处理器处理请求之前对该请求进行拦截处理
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//logger.info("Interceptor preHandle");
		
		//验证登录
		StringBuffer requestURL = request.getRequestURL();  
		logger.info(requestURL.toString());
		String loginname  = StaticUtil.null2String(request.getSession().getAttribute("loginname"));
		if (loginname!=null && loginname.length()>1) { 
			request.setAttribute("currentTimeMillis", System.currentTimeMillis());
			logger.info("longin username :"+loginname);
			
			
			return true; 
        } else {  
        	response.sendRedirect("../redirect.jsp"); // 返回页面  
        	return false; 
        } 
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object o, ModelAndView mav)
			throws Exception {
		//logger.info("postHandle");
		
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object o, Exception excptn)
			throws Exception {
		long pre = (Long) request.getAttribute("currentTimeMillis");
		long after = System.currentTimeMillis();
		logger.info("response time :"+(after-pre));
	}

}
